package cn.tedu.blackhorse.service.impl;


import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.StudentMapper;
import cn.tedu.blackhorse.mapper.StudentTeacherMapper;
import cn.tedu.blackhorse.pojo.dto.StudentAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.StudentUpdateDTO;
import cn.tedu.blackhorse.pojo.entity.Student;
import cn.tedu.blackhorse.pojo.entity.StudentTeacher;
import cn.tedu.blackhorse.pojo.vo.StudentListItemVO;
import cn.tedu.blackhorse.pojo.vo.StudentStandardVO;
import cn.tedu.blackhorse.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理品牌业务的实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Service
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentTeacherMapper studentTeacherMapper;


    public StudentServiceImpl() {
        log.info("创建业务对象：BrandServiceImpl");
    }


    @Override
    public void addNew(StudentAddNewDTO studentAddNewDTO) {
        int count = studentMapper.countByStudentId(studentAddNewDTO.getStudentNumber());
        if (count != 0){
            String message = "添加失败,学号已被使用";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Student student = new Student();
        BeanUtils.copyProperties(studentAddNewDTO,student);
        int row = studentMapper.insert(student);
        if (row != 1){
            String message = "添加失败,系统繁忙,请稍后重试[1]";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        StudentTeacher s = new StudentTeacher();
        s.setStudentId(studentAddNewDTO.getId());
        s.setTeacherId(studentAddNewDTO.getTeacherId());
        int insert = studentTeacherMapper.insert(s);
        if (insert == 0){
            String message = "添加失败,系统繁忙,请稍后重试[0]";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

    }

    @Override
    public void deleteById(Long id) {
        StudentStandardVO studentStandardVO = studentMapper.getById(id);
        if (studentStandardVO == null){
            String message = "删除失败,删除的学生未找到";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row = studentMapper.deleteById(id);
        if (row != 1){
            String message = "删除失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        int i = studentTeacherMapper.deleteByStudentId(id);
        if (i != 1){
            String message = "删除失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void updateById(Long id, StudentUpdateDTO studentUpdateDTO) {
        StudentStandardVO studentStandardVO = studentMapper.getById(id);
        if (studentStandardVO == null){
            String message = "修改失败,删除的学生未找到";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Student student = new Student();
        BeanUtils.copyProperties(studentUpdateDTO,student);
        student.setId(id);
        int row = studentMapper.updateById(student);
        if (row != 1){
            String message = "修改失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public List<StudentListItemVO> list() {
        return studentMapper.list();
    }
}
